//
//  ShenQingJiaRuViewController.m
//  YaoHeAPP
//
//  Created by 杨亚森 on 16/7/6.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ShenQingJiaRuViewController.h"
#import "Controller.h"
#import "Define.h"
#import "SVPullToRefresh.h"
#import "ShenQingJiaRuCell.h"
#import "ShenQingJiaRuItemModel.h"
#import "MJExtension.h"
#import "labelview.h"
#define kShenQingJiaRuCell @"ShenQingJiaRuCell"

@interface ShenQingJiaRuViewController ()<UITableViewDataSource,UITableViewDelegate>
{
  NSMutableArray*_dataArray;//数据源数组
    labelview *la;
}
@property(nonatomic,strong) UITableView *tableview;
@end

@implementation ShenQingJiaRuViewController

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [[self rdv_tabBarController] setTabBarHidden:YES animated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor=bgVCcolor;
    _dataArray=[[NSMutableArray alloc]init];
    //创建表格
    _tableview=[[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height-40) style: UITableViewStylePlain];
    _tableview.dataSource=self;
    _tableview.delegate=self;
    _tableview.backgroundColor=bgVCcolor;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableview.autoresizingMask = UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;//确保TablView能够正确的调整大小
    //_tableView.scrollEnabled=NO;
    [self.view addSubview:_tableview];
    
    //放置下拉上拉刷新控件
    __weak ShenQingJiaRuViewController *weakSelf = self;
    // setup pull-to-refresh
    [self.tableview addPullToRefreshWithActionHandler:^{
        [weakSelf insertRowAtTop];
    }];
    
    [self insertRowAtTop];
    
    UIView *bottomView=[[UIView alloc]initWithFrame:CGRectMake(0, self.view.frame.size.height-40-100, SCREEN_WIDTH, 40)];
    bottomView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bottomView];
    
    //全部拒绝
    UIButton *allRefused=[[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-180, 10,80, 20)];
    allRefused.backgroundColor=[UIColor whiteColor];
    [allRefused setTitle:@"全部拒绝" forState:UIControlStateNormal];
    [allRefused setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    allRefused.layer.borderColor=[UIColor redColor].CGColor;
    allRefused.layer.borderWidth=1;
    allRefused.titleLabel.font=Font(13);
    [allRefused addTarget:self action:@selector(allRefusedAction) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:allRefused];

    
    //全部拒绝
    UIButton *allagree=[[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH-90,10, 80, 20)];
    allagree.backgroundColor=RGBCOLOR(20, 160, 245);
    [allagree setTitle:@"全部同意" forState:UIControlStateNormal];
    allagree.titleLabel.font=Font(13);
    [allagree setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [allagree addTarget:self action:@selector(allagreeAction) forControlEvents:UIControlEventTouchUpInside];
    [bottomView addSubview:allagree];
    
    
}

-(void)insertRowAtTop
{
    
    __weak ShenQingJiaRuViewController *weakSelf = self;
    
    [weakSelf.tableview beginUpdates];
    
    [Controller requestWithURL:PostQueryApplyingUsers jsondic:nil finishBlock:^(ClassData *classData) {
        if ([classData.number integerValue]==0) {
            
            _dataArray=[ShenQingJiaRuItemModel mj_objectArrayWithKeyValuesArray:classData.data];
            //判断 是否  有 无数据
            if (_dataArray.count==0) {
                if (la) {
                    [la removeFromSuperview];
                }
                
                la=[[labelview alloc]init];
                [la setimage:@"Nodata_wenhao" tit:@"暂无数据" buttontit:nil frem:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
                la.backgroundColor= [UIColor colorWithRed:230/255.0 green:232/255.0 blue:241/255.0  alpha:1];
                la.image.frame=CGRectMake((self.view.frame.size.width-100)/2, 50, 100, 125);
                la.label.frame=CGRectMake(50, 180, self.view.frame.size.width-100, 30);
                [_tableview addSubview:la];
            }
            
            [_tableview reloadData];
        }
        else{
            ALERTSHOW(classData.message)
        }
        
    }];
    // 3. 结束刷新(相当于动画)
    [weakSelf.tableview endUpdates];
    [weakSelf.tableview.pullToRefreshView stopAnimating];

}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 70;
}

//每个分区单元格的个数
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataArray.count;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShenQingJiaRuCell *cell = [tableView dequeueReusableCellWithIdentifier:kShenQingJiaRuCell];
    if (!cell) {
        cell = [[ShenQingJiaRuCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:kShenQingJiaRuCell];
    }
    ShenQingJiaRuItemModel *shenQingJiaRuItemModel=_dataArray[indexPath.row];
    cell.shenQingJiaRuItemModel=shenQingJiaRuItemModel;
    cell.shenQingJiaRuCellBlock = ^() {
    
        //同意该人加入
        NSDictionary*dic=@{@"USER_IDS":shenQingJiaRuItemModel.USER_ID};
        [Controller requestWithURL:PostAllowApply jsondic:dic finishBlock:^(ClassData *classData) {
            
            if ([classData.number integerValue]==0) {
                //重新刷新数据
                [self insertRowAtTop];
                ALERTSHOW(@"成功加入");
            }
            else{
                ALERTSHOW(classData.message);
            }
        }];

        
        
    };
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //进入职员详情页面;

}


//要求委托方的编辑风格在表视图的一个特定的位置。
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCellEditingStyle result = UITableViewCellEditingStyleNone;//默认没有编辑风格
    if ([tableView isEqual:_tableview]) {
        result = UITableViewCellEditingStyleDelete;//设置编辑风格为删除风格
    }
    return result;
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated{//设置是否显示一个可编辑视图的视图控制器。
    [super setEditing:editing animated:animated];
    [_tableview setEditing:editing animated:animated];//切换接收者的进入和退出编辑模式。
}

- (NSString*)tableView:(UITableView*)tableView
titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath*)indexpath {
    return @"删除";
}
-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        
        [MobClick event:@"removeBlacklist"];
        ShenQingJiaRuItemModel *shenQingJiaRuItemModel=_dataArray[indexPath.row];
        //同意该人加入
        NSDictionary*dic=@{@"USER_IDS":shenQingJiaRuItemModel.USER_ID};
        [Controller requestWithURL:PostRefuseApply jsondic:dic finishBlock:^(ClassData *classData) {
            
            if ([classData.number integerValue]==0) {
                //重新刷新数据
                [self insertRowAtTop];
                ALERTSHOW(@"成功移除并拒绝");
            }
            else{
                ALERTSHOW(classData.message);
            }
        }];

        
        
    }
    
}

//全部同意
-(void)allagreeAction{

    if (_dataArray.count>0) {
        NSMutableArray *user_idArray=[NSMutableArray array];
        for (int i=0; i<_dataArray.count; i++) {
            ShenQingJiaRuItemModel *shenQingJiaRuItemModel=_dataArray[i];
            [user_idArray addObject:shenQingJiaRuItemModel.USER_ID];
        }
         NSString *user_idStr=[user_idArray componentsJoinedByString:@","];
         NSDictionary*dic=@{@"USER_IDS":user_idStr};
         [Controller requestWithURL:PostAllowApply jsondic:dic finishBlock:^(ClassData *classData) {
            
            if ([classData.number integerValue]==0) {
                //重新刷新数据
                [self insertRowAtTop];
                ALERTSHOW(@"成功加入");
            }
            else{
                ALERTSHOW(classData.message);
            }
        }];

    }

}

//全部拒绝
-(void)allRefusedAction{

  if (_dataArray.count>0) {
        NSMutableArray *user_idArray=[NSMutableArray array];
        for (int i=0; i<_dataArray.count; i++) {
            ShenQingJiaRuItemModel *shenQingJiaRuItemModel=_dataArray[i];
            [user_idArray addObject:shenQingJiaRuItemModel.USER_ID];
        }
        NSString *user_idStr=[user_idArray componentsJoinedByString:@","];
        NSDictionary*dic=@{@"USER_IDS":user_idStr};
        [Controller requestWithURL:PostRefuseApply jsondic:dic finishBlock:^(ClassData *classData) {
            
            if ([classData.number integerValue]==0) {
                //重新刷新数据
                [self insertRowAtTop];
                ALERTSHOW(@"成功移除并拒绝");
            }
            else{
                ALERTSHOW(classData.message);
            }
        }];
    }
    
}

@end
